"""
Case Type   : datakit安全中心
Case Name   : 删除角色
Create At   : 2025/2/7
Owner       : @sungang14
Description :
    1. 打开安全中心-角色与权限页面
    2. 点击修改某个角色
    3. 修改角色的名称
    4. 查看账号管理页面对应账号角色显示【结果1】
Expect      :
    1、【结果1】账号角色名称同步改变
History     :
"""

import os
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger
from testcase.utils.datakit_api.Api import Api


class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.com_sh = CommonSH()
        self.token = self.com_sh.get_token_value(macro.DATAKIT_PASSWD)
        self.assertNotEqual("error", self.token)
        self.sys_api = Api(self.token)
        self.role_name = 'sgtest_role0023'
        self.role_newname = 'sgtest_newrole0023'
        self.user_name = 'sgtest_user0023'
        self.data_role = f"pageNum=1&pageSize=10&roleName=" + self.role_name

        text = '----查询待添加角色是否存在，存在则删除----'
        self.log.info(text)
        res1 = self.sys_api.query_role_list(self.data_role)
        self.log.info(res1.json())
        self.num_role = res1.json()['total']
        self.log.info(self.num_role)
        if self.num_role != 0:
            self.role_id = res1.json()['rows'][0]['roleId']
            res7 = self.sys_api.delete_role([self.role_id])
            self.log.info(res7.json())
            self.assertEqual(200, res7.json()['code'], '删除角色失败')
            self.assertEqual("success", res7.json()['msg'], '删除用户失败')

        text = '----step1:安全中心-角色与权限菜单，点击“添加角色”，点击确定【结果1】 expect:成功----'
        self.log.info(text)
        self.data1 = {"menuIds": [],
                      "roleName": self.role_name,
                      "status": f"0",
                      "remark":""}
        res1 = self.sys_api.add_role(self.data1)
        self.log.info(res1.json())
        self.assertEqual(200, res1.json()['code'], '添加角色失败')
        self.assertEqual("success", res1.json()['msg'], '添加角色失败')

        text = '----step2:查询添加角色id expect:查询成功----'
        self.log.info(text)
        res2 = self.sys_api.query_role_list(self.data_role)
        self.log.info(res2.json())
        self.assertEqual(1, res2.json()['total'], '添加角色失败')
        self.role_id = res2.json()['rows'][0]['roleId']
        self.log.info(self.role_id)

        text = '----step3:安全中心-账号管理，点击“添加账号” expect:成功----'
        self.log.info(text)
        self.data3 = {"userName": self.user_name,
                      "password": f"sBfBGhAg4+NypfvYDSAdyvZPx/PAW0NInwVdN3IGNHfj62ltrvonFdhGFEti8MIRajnN0mlbUsUktMOlgIHBWih4ffAEaDcCI+uN1jrQAigQcrOLwNZHkb3pRfQyelWpx2A5LgEhuGmJ6vPN13DSd+C3D5uMGZhGG7BEQPSGomk=",
                      "nickName": self.user_name,
                      "phonenumber": f"16666666666",
                      "roleIds": int(self.role_id),
                      "status": "0"}
        res3 = self.sys_api.add_user(self.data3)
        self.log.info(res3.json())
        self.assertEqual(200, res3.json()['code'], '添加账号失败')
        self.assertEqual("success", res3.json()['msg'], '添加账号失败')

    def test_batch_installation(self):
        text = '----step4:安全中心-角色与权限菜单，确定对应角色点击“修改”操作，修改角色名称 expect:修改成功----'
        self.log.info(text)
        self.data4 = {"roleId": int(self.role_id),
                      "menuIds": [6, 605],
                      "roleName": self.role_newname,
                      "status": f"0",
                      "remark": ""}
        res4 = self.sys_api.update_role(self.data4)
        self.log.info(res4.json())
        self.assertEqual(200, res4.json()['code'], '更新角色失败')
        self.assertEqual("success", res4.json()['msg'], '更新角色失败')
        text = '----step5:查看账号管理页面对应账号角色显示 expect:账号角色名称同步改变----'
        self.log.info(text)
        self.data5 = f"userName=" + self.user_name
        res5 = self.sys_api.query_user_page(self.data5)
        self.log.info(res5.json())
        self.assertEqual(1, res5.json()['total'], '查询账号失败')
        self.user_id = res5.json()['rows'][0]['roleName']
        self.log.info(self.user_id)

    def tearDown(self):
        text = '----:查询账号user_id expect:查询成功----'
        self.log.info(text)
        self.data5 = f"userName=" + self.user_name
        res5 = self.sys_api.query_user_page(self.data5)
        self.log.info(res5.json())
        self.assertEqual(1, res5.json()['total'], '查询账号失败')
        self.user_id = res5.json()['rows'][0]['userId']
        self.log.info(self.user_id)
        text = '----:删除账号 expect：删除成功----'
        self.log.info(text)
        self.data6 = [self.user_id]
        res6 = self.sys_api.delete_user(self.data6)
        self.log.info(res6.json())
        self.assertEqual(200, res6.json()['code'], '删账号失败')
        self.assertEqual("success", res6.json()['msg'], '删除账号失败')
        self.log.info(f"-----{os.path.basename(__file__)} finsh-----")
        text = '----:删除角色 expect：删除成功----'
        self.log.info(text)
        self.data7 = [self.role_id]
        res7 = self.sys_api.delete_role(self.data7)
        self.log.info(res7.json())
        self.assertEqual(200, res7.json()['code'], '删除角色失败')
        self.assertEqual("success", res7.json()['msg'], '删除用户失败')
        self.log.info(f"-----{os.path.basename(__file__)} finsh-----")
